package com.ydsungan.service.impl;

import com.ydsungan.common.DateUtils;
import com.ydsungan.dao.StatisticsMapper;
import com.ydsungan.dao.UserMapper;
import com.ydsungan.pojo.StatisticsExample;
import com.ydsungan.pojo.User;
import com.ydsungan.pojo.UserExample;
import com.ydsungan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public User selectById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int saveUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<User> getAllUser() {
        UserExample userExample = new UserExample();
        return userMapper.selectByExample(userExample);
    }

    @Override
    public long countUsers() {
        UserExample userExample = new UserExample();
        return userMapper.countByExample(userExample);
    }

    @Override
    public long countTodayActiveUsers() throws ParseException {
        StatisticsExample statisticsExample = new StatisticsExample();
        StatisticsExample.Criteria criteria = statisticsExample.createCriteria();
        List<Date> dateList = DateUtils.getDateStartAndEnd(0);
        criteria.andCreateTimeBetween(dateList.get(0),dateList.get(1));
        return statisticsMapper.countByExample(statisticsExample);
    }


}
